import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

interface Response<T> {
  data: T;
}
interface ResponseType<T> {
  data: {
    data: T;
    code: string;
    extra?: any;
    message: string | null;
  };
}

@Injectable()
// 响应数据类型转化
export class TransformInterceptor<T>
  implements NestInterceptor<T, Response<T>>
{
  intercept(
    context: ExecutionContext,
    next: CallHandler,
  ): Observable<Response<T>> {
    return next.handle().pipe(
      map((data) => ({
        data,
        code: '0000',
        extra: {},
        message: 'success',
        status: 200,
      })),
    );
  }
}
